﻿using System;
using System.Configuration;
using DuLabs.Classic.Configuration;

namespace DuLabs.Classic.Logging
{
    public static class Logger
    {
        public static void Write(string message)
        {
            try
            {
                Logger.Write(-521, string.Empty, message);
            }
            catch
            {
                throw;
            }
        }

        public static void Write(int logType, string createBy, string message)
        {
            try
            {
                LogEntry logEntry = new LogEntry();
                logEntry.LogType = logType;
                logEntry.CreateBy = createBy;
                logEntry.CreateDate = DateTime.Now;
                logEntry.Message = message;

                Logger.Write(logEntry);
            }
            catch
            {
                throw;
            }
        }

        public static void Write(int logType, string createBy, string message, Exception exception)
        {
            try
            {
                LogEntry logEntry = new LogEntry();
                logEntry.LogType = logType;
                logEntry.CreateBy = createBy;
                logEntry.CreateDate = DateTime.Now;
                logEntry.Message = message;
                logEntry.Exception = exception;

                Logger.Write(logEntry);
            }
            catch
            {
                throw;
            }
        }

        public static void Write(LogEntry logEntry)
        {
            // 自定义配置节路径
            string sectionName = "dulabs/dulabs.classic.logging";

            // 获取自定义配置节
            LoggingSection section = ConfigurationManager.GetSection(sectionName) as LoggingSection;

            if (section == null)
            {
                throw new ApplicationException(string.Format("Configuration section error: {0}", sectionName));
            }

            if (!section.Enabled)
            {
                return;
            }

            Exception logWriterException = null;

            foreach (ClassElement classElement in section.LogWriters)
            {
                try
                {
                    if (!classElement.Enabled)
                    {
                        continue;
                    }

                    object obj = Activator.CreateInstance(classElement.Assembly, classElement.Type).Unwrap();

                    ILogWriter logWriter = obj as ILogWriter;

                    if (logWriter != null)
                    {
                        logWriter.Write(logEntry, classElement.Params);
                    }
                }
                catch (Exception ex)
                {
                    if (logWriterException == null)
                    {
                        logWriterException = ex;
                    }
                }
            }

            if (logWriterException != null && section.ThrowException)
            {
                throw logWriterException;
            }
        }        
    }
}
